/*
 * NewCattle.h
 *
 *  Created on: 20/11/2012
 *      Author: gontijo
 */

#include "cc430x613x.h"
#include "RF1A.h"
#include "hal_PMM.h"
#include "hal_UCS.h"

/*********************************
* Defines
*/
#define  PACKET_LEN         (10)	    	// PACKET_LEN > FIFO Size
#define  RSSI_IDX           (PACKET_LEN+1)  // Index of appended RSSI
#define  CRC_LQI_IDX        (PACKET_LEN+2)  // Index of appended LQI, checksum
#define  CRC_OK             (BIT7)          // CRC_OK bit
#define  PATABLE_VAL        (0x51)          // 0 dBm output
#define  RX_TIMER_PERIOD    (85)
#define  TX_TIMER_PERIOD    (40)

#define CC430_STATE_TX                   (0x20)
#define CC430_STATE_IDLE                 (0x00)
#define CC430_STATE_TX_UNDERFLOW         (0x70)
#define CC430_STATE_MASK                 (0x70)
#define CC430_FIFO_BYTES_AVAILABLE_MASK  (0x0F)
#define CC430_STATE_RX                   (0x10)
#define CC430_STATE_RX_OVERFLOW          (0x60)

#define MIN(n,m) (((n) < (m)) ? (n) : (m))

/*******************
 * Function Definition
 */
void TransmitPacket(void);
void ReceivePacket(void);
void ReceiveOn(void);
void ReceiveOff(void);
void InitButtonLeds(void);
void InitRadio(void);
void InitTimer(void);
void pktTxHandler(void);
void pktRxHandler(void);

// == If is a Node or a RBS
#define NCTTLP_Node			(0x00)

// === Main CMDs ===
#define NCTTLP_FLAG			(0x7E)
#define NCTTLP_QUERY		(0x70)
#define NCTTLP_DATA			(0x90)
#define NCTTLP_TID			(0xE1)

// === Query CMDs ===
#define NCTTLP_QUERY_ACK	(0x71)
#define NCTTLP_QUERY_TID	(0x73)
#define NCTTLP_QUERY_RSSI	(0x75)
#define NCTTLP_QUERY_ACC	(0x77)
#define NCTTLP_QUERY_SEARCH	(0x79)

// === DATA RETURN CMDs ===
#define NCTTLP_DATA_ACK		(0x91)
#define NCTTLP_DATA_TID		(0x93)
#define NCTTLP_DATA_RSSI	(0x95)
#define NCTTLP_DATA_ACC		(0x97)
#define NCTTLP_DATA_SEARCH	(0x99)
#define NCTTLP_DATA_eTID	(0x9B)
#define NCTTLP_DATA_eACK	(0x9D)

// === Errors Codes ===
#define NCTTLP_ERRLEN		(0x30)
#define NCTTLP_ERRRECP		(0x33)

#define MIN(n,m) (((n) < (m)) ? (n) : (m))

typedef unsigned char u8;

extern unsigned char packetReceived;
extern unsigned char packetTransmit;

extern unsigned char RxBuffer[PACKET_LEN+2];
extern unsigned char RxBufferLength;
extern unsigned char TxBuffer[PACKET_LEN];
extern unsigned char buttonPressed;
extern unsigned int i;

extern unsigned char searching;
extern unsigned char ack_ok;
extern unsigned char responder;

extern unsigned char transmitting;
extern unsigned char receiving;

void NCTTLP_Core_SM(unsigned char state);
unsigned char TrataPkt(void );

